home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 1.iso / dist / fw_mysql.idb / usr / freeware / bin / safe_mysqld.z / safe_mysqld
Text File  |  1999-10-18  |  4KB  |  150 lines

  1. #!/bin/sh
  2. # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
  3. # This file is public domain and comes with NO WARRANTY of any kind
  4. #
  5. # scripts to start the MySQL daemon and restart it if it dies unexpectedly
  6. #
  7. # This should be executed in the MySQL base directory if you are using a
  8. # binary installation that has other paths than you are using.
  9. #
  10. # mysql.server works by first doing a cd to the base directory and from there
  11. # executing safe_mysqld
  12.  
  13. trap '' 1 2 3 15            # we shouldn't let anyone kill us
  14.  
  15. # Check if we are starting this relative (for the binary release)
  16. if test -d ./data/mysql -a -f ./share/mysql/english/errmsg.sys -a \
  17.  -x ./bin/mysqld
  18. then
  19.   MY_BASEDIR_VERSION=`pwd`        # Where bin, share and data is
  20.   DATADIR=$MY_BASEDIR_VERSION/data    # Where the databases are
  21.   ledir=$MY_BASEDIR_VERSION/bin        # Where mysqld are
  22. # Check if this is a 'moved install directory'
  23. elif test -f ./var/mysql/db.frm -a -f ./share/mysql/english/errmsg.sys -a \
  24.  -x ./libexec/mysqld
  25. then
  26.   MY_BASEDIR_VERSION=`pwd`        # Where libexec, share and var is
  27.   DATADIR=$MY_BASEDIR_VERSION/var    # Where the databases are
  28.   ledir=$MY_BASEDIR_VERSION/libexec    # Where mysqld are
  29. else
  30.   MY_BASEDIR_VERSION=/usr/freeware
  31.   DATADIR=/var/spool/mysql
  32.   ledir=/usr/freeware/sbin
  33.   if test ! -x $ledir/mysqld
  34.   then
  35.     echo "The file $ledir/mysqld doesn't exists or is not executable"
  36.     echo "Please do a cd to the mysql installation directory and restart"
  37.     echo "this scrift from there as follows:"
  38.     echo "./bin/safe_mysqld".
  39.     exit 1;
  40.   fi
  41. fi
  42.  
  43. pid_file=$DATADIR/`/usr/bsd/hostname`.pid
  44. log=$DATADIR/`/usr/bsd/hostname`.log
  45. err_log=$DATADIR/`/usr/bsd/hostname`.err
  46.  
  47. # Parse arguments to see if caller wants the pid_file somewhere else.
  48. for arg
  49. do
  50.   case $arg
  51.     in
  52.     --pid-file=*)
  53.       pid_file=`echo "$arg" | sed -e "s;--pid-file=;;"`
  54.       ;;
  55.   esac
  56. done
  57.  
  58. #
  59. # If there exists an old pid file, check if the daemon is already running
  60. # Note: The switches to 'ps' may depend on your operating system
  61.  
  62. if test -f $pid_file
  63. then
  64.   PID=`cat $pid_file`
  65.   if /bin/kill -0 $PID
  66.   then
  67.     if /sbin/ps -ef | grep mysqld | grep " $PID " > /dev/null
  68.     then    # The pid contains a mysqld process
  69.       echo "A mysqld process already exists"
  70.       echo "A mysqld process already exists at " `date` >> $err_log
  71.       exit 1;
  72.     fi
  73.   fi
  74.   rm -f $pid_file
  75.   if test -f $pid_file
  76.   then
  77.     echo "Fatal error: Can't remove the pid file: $pid_file"
  78.     echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
  79.     echo "Please remove it manually and start $0 again"
  80.     echo "mysqld daemon not started"
  81.     exit 1;
  82.   fi
  83. fi
  84.  
  85. echo "Starting mysqld daemon with databases from $DATADIR"
  86.  
  87. #Default communication ports
  88. #MYSQL_TCP_PORT=3306
  89. if test -z "$MYSQL_UNIX_PORT"
  90. then
  91.   MYSQL_UNIX_PORT="/tmp/mysql.sock"
  92.   export MYSQL_UNIX_PORT    
  93. fi
  94. #export MYSQL_TCP_PORT
  95.  
  96. # Does this work on all systems?
  97. #if type ulimit | grep "shell builtin" > /dev/null
  98. #then
  99. #  ulimit -n 256 > /dev/null 2>&1        # Fix for BSD and FreeBSD systems
  100. #fi
  101.  
  102. echo "mysqld started on " `date` >> $err_log
  103. while true
  104. do
  105.   rm -f $MYSQL_UNIX_PORT $pid_file    # Some extra safety
  106.   if test "$#" -eq 0
  107.   then
  108.     nohup $ledir/mysqld --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR \
  109.       >> $err_log 2>&1
  110.   else
  111.     nohup $ledir/mysqld --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR \
  112.       "$@" >> $err_log 2>&1
  113.   fi
  114.   if test ! -f $pid_file        # This is removed if normal shutdown
  115.   then
  116.     break;
  117.   fi
  118.   if false
  119.   then
  120.     # Test if one proces was hanging.
  121.     # This is only a fix for Linux (running as base 3 mysqld processes)
  122.     # but should work for the rest of the servers.
  123.     # The only thing is ps x => redhat 5 gives warnings when using ps -x.
  124.     # kill -9 is used or the proces won't react on the kill.
  125.     numofproces=`ps x | grep -v "grep" | grep -c $ledir/mysqld`
  126.     echo -e "\nNumber of processes running now: $numofproces" | tee -a $err_log
  127.     I=1
  128.     while test "$I" -le "$numofproces"
  129.     do 
  130.       PROC=`ps x | grep $ledir/mysqld | grep -v "grep" | tail -1` 
  131.     for T in $PROC
  132.     do
  133.       break
  134.     done
  135.     #    echo "TEST $I - $T **"
  136.     if kill -9 $T
  137.     then
  138.       echo "mysqld proces hanging, pid $T - killed" | tee -a $err_log
  139.     else 
  140.       break
  141.     fi
  142.     I=`expr $I + 1`
  143.     done
  144.   fi
  145.   echo "mysqld restarted on " `date` | tee -a $err_log
  146. done
  147.  
  148. echo "mysqld ended on " `date` >> $err_log
  149. echo "mysqld daemon ended"
  150.